#include "test.h"

void Put(char* img, int i, int j, char c,int w) 
{
	char* p = img + j * w + i;
	if (*p == ' ' || *p == '_')
		*p = c;
}

void KochCurve(char* img,int* x,int* y,int n, int dir,int w) 
{
	if (n == 0)
	{
		switch (dir % 6)
		{
		case 0: 
			Put(img,(*x)++, *y, '_',w);
			Put(img,(*x)++, *y, '_', w);
			break;
		case 1: 
			Put(img,(*x)++, (*y)--, '/', w);
			break;
		case 2: 
			Put(img,--(*x), (*y)--, '\\', w);
			break;
		case 3: 
			Put(img,--(*x), *y, '_', w);
			Put(img,--(*x), *y, '_', w);
			break;
		case 4: 
			Put(img,--(*x), ++(*y), '/', w);
			break;
		case 5: 
			Put(img,(*x)++, ++(*y), '\\', w);
			break;
		}
	}
	else 
	{
		KochCurve(img,x,y,n - 1, dir, w);
		KochCurve(img, x, y, n - 1, dir + 1, w);
		KochCurve(img, x, y, n - 1, dir + 5, w);
		KochCurve(img, x, y, n - 1, dir, w);
	}
}
void PrintSnow(int i) 
{
	int w = 2;
	int h = 4;
	for (int n = 0; n < i; n++) 
	{
		char* img = (char*)malloc(sizeof(char) * w * h);
		if (img == NULL)
			return;
		memset(img, ' ', w * h);
		int x  = 0;
		int y  = h / 4 - 1;
		KochCurve(img, &x, &y, n, 0, w);
		KochCurve(img, &x, &y, n, 4, w);
		KochCurve(img, &x, &y, n, 2, w);
		printf("\n\n");
		for (int j = 0; j < h; j++)
		{
			for (int i = 0; i < w; i++)
				printf("%c",img[j *w + i]);
			printf("\n");
		}
		w *= 3;
		h = n == 0 ? 4 : h * 3;
		free(img);
		img = NULL;
		Sleep(300);
		system("cls");
	}
}
